﻿using System;
using System.Collections.Generic;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Config;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Ads
{
    public partial class List : BaseControl
    {
        public string MENUNAME = string.Empty;

        public List()
        {
        }

        public List(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCommandName.Text = MENUNAME;
                List<ADSItem> lstAds = ManagerFactory.DAOEntity.GetList(new ADSItem() { PORTALID = Session.WorkingPortal().PORTALID });
                if (lstAds != null)
                    AdsAccess.LoadAds(lstAds, tvwControls.Nodes);
                drpITEMTYPE.DataSource = StaticData.MediaType;
                drpITEMTYPE.DataBind();
                List<PortalUser> lstUser = ManagerFactory.PortalUserManager.GetAll();
                PortalAccess.LoadUsers(lstUser, drpUSERID.Items);
            }
        }

        protected void tvwControls_SelectedNodeChanged(object sender, EventArgs e)
        {
            GetInfo(Convert.ToInt32(tvwControls.SelectedValue));
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                ManagerFactory.DAOEntity.Insert(GetData());
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                GetDefault();
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(lblID.Text))
            {
                ADSItem info = GetData();
                info.ID = Convert.ToInt32(lblID.Text);
                ManagerFactory.DAOEntity.Update(info);
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                GetDefault();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(lblID.Text))
            {
                ManagerFactory.DAOEntity.Delete(new ADSItem { ID = Convert.ToInt32(lblID.Text) });
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                GetDefault();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GetDefault();
        }

        private void GetInfo(int ID)
        {
            ADSItem info = ManagerFactory.DAOEntity.GetInfo(new ADSItem() { ID = ID });
            if (info != null)
            {
                lblID.Text = info.ID.ToString();
                txtNAME.Text = info.NAME;
                txtITEMADDRESS.Text = info.PARTNERLINK;
                txtIMAGES.Text = info.SOURCE;
                drpUSERID.SelectedValue = info.USERID.ToString();
                drpITEMTYPE.SelectedValue = info.TYPE.ToString();
                txtDatetime.Text = info.WAITINGDATE.ToString();
                txtExprisedDate.Text = (DateTime.MaxValue == info.EXPRISEDDATE)
                                           ? string.Empty
                                           : info.EXPRISEDDATE.ToString();
                lblUpdateStatus.Text = string.Empty;
            }
        }

        private void GetDefault()
        {
            lblID.Text = string.Empty;
            txtNAME.Text = string.Empty;
            txtITEMADDRESS.Text = string.Empty;
            txtIMAGES.Text = string.Empty;
            drpUSERID.SelectedIndex = -1;
            drpITEMTYPE.SelectedIndex = -1;
            lblUpdateStatus.Text = string.Empty;
            txtDatetime.Text = DateTime.Now.ToString();
            txtExprisedDate.Text = string.Empty;
            List<ADSItem> lstAds = ManagerFactory.DAOEntity.GetList(new ADSItem() { PORTALID = Session.WorkingPortal().PORTALID });
            if (lstAds != null)
                AdsAccess.LoadAds(lstAds, tvwControls.Nodes);
        }

        private ADSItem GetData()
        {
            var info = new ADSItem();
            info.NAME = txtNAME.Text;
            info.PARTNERLINK = txtITEMADDRESS.Text;
            info.SOURCE = txtIMAGES.Text;
            info.USERID = Convert.ToInt32(drpUSERID.SelectedValue);
            info.PORTALID = Session.WorkingPortal().PORTALID;
            info.TYPE = Convert.ToInt32(drpITEMTYPE.SelectedValue);
            info.WAITINGDATE = string.IsNullOrEmpty(txtDatetime.Text)
                                   ? DateTime.Now
                                   : Convert.ToDateTime(txtDatetime.Text);
            info.EXPRISEDDATE = string.IsNullOrEmpty(txtExprisedDate.Text)
                                    ? DateTime.MaxValue
                                    : Convert.ToDateTime(txtExprisedDate.Text);
            return info;
        }
    }
}